import { onMounted, ref } from "vue";
import { useRoute } from "vue-router";
import { RequestProductDetailById } from "@/api/product";
import type { Ref } from "vue";
import { useRouter } from "vue-router";
export const useProduct = () => {
  interface IProduct {
    id: number;
    picture: string;
    shop: string;
    price: number;
    product: string;
  }
  const router = useRouter();
  const route = useRoute();

  let product: Ref<IProduct> = ref({
    id: 0,
    shop: "",
    product: "",
    picture: "",
    price: 0,
  });
  const productList:string=ref({})
  const onClickLeft = () => {
    history.back();
  };
  // 跳转购物车
  const onCart = () => {
    router.push({ path: "/cart" });
  };

  /**
   * 商品详情
   */
  const getProductDetail = async (id: number) => {
    let data = await RequestProductDetailById(id);
    let { resultCode, resultInfo } = data;
    if (resultCode == 1) {
      product.value = resultInfo;
      productList.value =  product.value.list
    }
  };

  onMounted(() => {
    let id: number = Number(route.params.id); //商品id
    getProductDetail(id);
  });

  return {
    onClickLeft,
    product,
    onCart,
    productList
  };
};
